﻿namespace Beetle
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class ByteArraySegment
    {
        internal byte[] _field_a;
        public int Count;
        public int Offset;

        public ByteArraySegment()
        {
        }

        public ByteArraySegment(int length)
        {
            this.Array =(new byte[length]);
        }

        public void Clear()
        {
            this.Array =(null);
        }

        public string Decoding(System.Text.Encoding coding)
        {
            return coding.GetString(this._field_a, this.Offset, this.Count);
        }

        public string Decoding(System.Text.Encoding coding, byte[] data, int poffset, int pcount)
        {
            return coding.GetString(data, poffset, pcount);
        }

        public void DecryptTo(ByteArraySegment segment, DESCryptoServiceProvider mDESProvider)
        {
            MemoryStream stream = new MemoryStream(this._field_a, this.Offset, this.Count);
            int index = 0;
            using (CryptoStream stream2 = new CryptoStream(stream, mDESProvider.CreateDecryptor(), CryptoStreamMode.Read))
            {
                for (int i = (byte) stream2.ReadByte(); i >= 0; i = stream2.ReadByte())
                {
                    segment._field_a[index] = (byte) i;
                    index++;
                }
                segment.SetInfo(0, index);
            }
        }

        public void Encoding(string value, System.Text.Encoding coding)
        {
            try
            {
                int count = coding.GetBytes(value, 0, value.Length, this.Array, 0);
                this.SetInfo(0, count);
            }
            catch (Exception exception)
            {
                throw NetTcpException.StringEncodingError(exception);
            }
        }

        public void EncryptTo(ByteArraySegment segment, DESCryptoServiceProvider mDESProvider)
        {
            MemoryStream stream = new MemoryStream(segment._field_a);
            using (CryptoStream stream2 = new CryptoStream(stream, mDESProvider.CreateEncryptor(), CryptoStreamMode.Write))
            {
                stream2.Write(this._field_a, this.Offset, this.Count);
                stream2.FlushFinalBlock();
                segment.SetInfo(0, (int) stream.Position);
                stream2.Close();
            }
        }

        public void FromBase64String(string value)
        {
            try
            {
                byte[] data = Convert.FromBase64String(value);
                this.Import(data, 0, data.Length);
            }
            catch (Exception exception)
            {
                throw NetTcpException.StringEncodingError(exception);
            }
        }

        public byte GetData(int index)
        {
            return this._field_a[this.Offset + index];
        }

        public void Import(BufferWriter writer)
        {
            int dstOffset = 0;
            int count = 0;
            while (writer._field_b.Count > 0)
            {
                using (Class_b _b = writer._field_b.Dequeue())
                {
                    try
                    {
                        Buffer.BlockCopy(_b._field_e, 0, this._field_a, dstOffset, _b._field_h);
                        dstOffset += _b._field_h;
                        count += _b._field_h;
                    }
                    catch (Exception exception)
                    {
                        throw exception;
                    }
                    continue;
                }
            }
            this.SetInfo(0, count);
        }

        public void Import(ByteArraySegment e)
        {
            this.Import(e.Array, e.Offset, e.Count);
        }

        public void Import(byte[] data, int offet, int count)
        {
            Buffer.BlockCopy(data, offet, this.Array, 0, count);
            this.SetInfo(0, count);
        }

        public void SetInfo(int offset, int count)
        {
            this.Offset = offset;
            this.Count = count;
        }

        public void SetInfo(byte[] data, int offset, int count)
        {
            this.Array=(data);
            this.Offset = offset;
            this.Count = count;
        }

        public string ToBase64String()
        {
            return Convert.ToBase64String(this._field_a, this.Offset, this.Count);
        }

        public string ToBase64String(byte[] data, int poffset, int pcount)
        {
            return Convert.ToBase64String(data, poffset, pcount);
        }

        public byte[] Array
        {
            get
            {
                return this._field_a;
            }
            private set
            {
                this._field_a = value;
            }
        }

        public int BufferLength
        {
            get
            {
                if (this.Array == null)
                {
                    return 0;
                }
                return this.Array.Length;
            }
        }
    }
}

